package com.bw.zyk.zhanggm.user.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bw.zyk.zhanggm.user.entity.User;
import com.bw.zyk.zhanggm.user.service.IUserService;
import com.macro.cloud.gitee.domain.GiteeClientUserDto;
import com.macro.cloud.gitee.service.Oauth2ClientService;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.ip.IpUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.redis.service.RedisService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zhanggm
 * @since 2024-04-23
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private Oauth2ClientService oauth2ClientService;

    @Autowired
    private IUserService iUserService;

    @Autowired
    private RedisService redisService;

    @GetMapping("test")
    public AjaxResult test() {
        return AjaxResult.error();
    }


    /**
     * gitee登录的跳转地址
     * @return
     */
    @GetMapping("getOauth2CodeUrl")
    public AjaxResult getOauth2CodeUrl() {
        String oauth2CodeUrl = oauth2ClientService.getOauth2CodeUrl();
        return AjaxResult.success(oauth2CodeUrl);
    }

    /**
     * 授权码登录接口
     * @param code
     * @return
     */
    @GetMapping("login")
    public AjaxResult login(String code) {
        String accessToken = oauth2ClientService.getAccessToken(code);
        GiteeClientUserDto currentUser = oauth2ClientService.getCurrentUser(accessToken);
        Integer id = currentUser.getId();
        //根据gitee的账号id，查询本地库的用户
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getGiteeUserId, id);
        User loginUser = iUserService.getOne(queryWrapper);
        if (loginUser == null) {
            return AjaxResult.error("账号不存在");
        }
        //判断ip，剔除异地登录用户
        String loginIP = IpUtils.getIpAddr();
        Object lastLoginIP = redisService.getCacheObject("login:" + loginUser.getId() + ":ip");
        //判断用户是否重复登录
        if (redisService.hasKey("login:" + loginUser.getId()) || !loginIP.equals(lastLoginIP)) {
            Object lastToken = redisService.getCacheObject("login:" + loginUser.getId());
            redisService.deleteObject("login:" + loginUser.getId());
            redisService.deleteObject("login:" + lastToken);
            redisService.deleteObject("login:" + loginUser.getId()+":ip");
        }
        //jwt生成token
        loginUser.setCreated(null);
        String token = JwtUtils.createToken(BeanUtil.beanToMap(loginUser));
        Claims claims = JwtUtils.parseToken(token);
        //缓存redis
//        redisService.setCacheObject("login:"+loginUser.getId()+":"+token, loginUser);
        redisService.setCacheObject("login:"+token, loginUser);
        redisService.setCacheObject("login:" + loginUser.getId(), token);
        redisService.setCacheObject("login:" + loginUser.getId()+":ip", loginIP);
        return AjaxResult.success(token);
    }

}
